package com.guogee.pdb.dao;

import com.guogee.pdb.bean.RLGatewayDevice;
import com.guogee.pdb.db.DBHelper.RLGatewayDeviceCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;

public class RLGatewayDeviceDAO extends ADao<RLGatewayDevice> {

	public RLGatewayDeviceDAO() {
		dbCollection = dbHelper.getIsmartDB().getCollection(RLGatewayDeviceCollection.COLLECTION_NAME);
	}

	@Override
	public boolean insertItem(RLGatewayDevice obj) {
		if(insertItem1(obj) != null){
			return true;
		}
		return false;
	}

	@Override
	public RLGatewayDevice parstItemFromCursor(DBCursor cursor) {
		return parstDBObject(cursor.next());
	}

	@Override
	public String insertItem1(RLGatewayDevice obj) {
		// TODO Auto-generated method stub
		DBObject dbObj = new BasicDBObject();
		dbObj.put(RLGatewayDeviceCollection.GW_SN, obj.getGwSn());
		dbObj.put(RLGatewayDeviceCollection.DEV_MAC, obj.getDevMac());
		dbObj.put(RLGatewayDeviceCollection.BIND_TIME, obj.getBindTime());
		dbObj.put(RLGatewayDeviceCollection.DEV_TYPE, obj.getDevType());
		if(checkWritenResult(dbCollection.insert(dbObj))){
			return dbObj.get(RLGatewayDeviceCollection.ID).toString();
		}
		return null;
	}

	@Override
	public RLGatewayDevice parstDBObject(DBObject object) {
		// TODO Auto-generated method stub
		RLGatewayDevice rl = new RLGatewayDevice();
		rl.setId(object.get(RLGatewayDeviceCollection.ID).toString());
		rl.setGwSn(object.get(RLGatewayDeviceCollection.GW_SN).toString());
		rl.setDevMac(object.get(RLGatewayDeviceCollection.DEV_MAC).toString());
		rl.setBindTime(Long.valueOf(object.get(RLGatewayDeviceCollection.BIND_TIME).toString()));
		rl.setDevType(Short.parseShort(object.get(RLGatewayDeviceCollection.DEV_TYPE).toString()));
		return rl;
	}
}
